//
//  TKPhotoSelectViewController.m
//  TKPluginDemo
//
//  Created by liupm on 16/1/12.
//  Copyright © 2016年 liupm. All rights reserved.
//

#import "TKIMImagePickerViewController.h"
#import "TKIMAlbumGroupViewController.h"
#import "TKIMImageLayerUtil.h"

@interface TKIMImagePickerViewController ()
@property(nonatomic,strong) TKIMImageLayerUtil *layerUtil;
@end

@implementation TKIMImagePickerViewController
- (instancetype)init
{
    self = [super init];
    if (self) {
        self.maxAllowSelect = 1;
        self.allowsMultipleSelection = YES;
        //设置navbar默认样式
        self.navigationBar.barTintColor = [UIColor colorWithRed:0.16 green:0.17 blue:0.21 alpha:1];
        self.navigationBar.tintColor = [UIColor whiteColor];
        self.navigationBar.titleTextAttributes = @{NSFontAttributeName:[UIFont boldSystemFontOfSize:18.0f],
                                                   NSForegroundColorAttributeName:[UIColor whiteColor]
                                                   };
    }
    return self;
}


-(void)loadView
{
    [super loadView];
    self.navigationBar.translucent = NO;
    TKIMAlbumGroupViewController *album = [[TKIMAlbumGroupViewController alloc] initWithStyle:UITableViewStylePlain];
    self.viewControllers = @[album];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:TKIMImagePickerCloseEvent object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(close:) name:TKIMImagePickerCloseEvent object:nil];
}


-(void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:TKIMImagePickerCloseEvent object:nil];
}


/**
 *  @Author lpm, 16-04-22 11:04:53
 *
 *  @brief  关闭
 */
-(void)close:(NSNotification *)notify;
{
    [self.layerUtil showLoading:@"图片处理中" inView:self.view];
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        NSMutableArray *assets = notify.object;
        for (TKIMPhotoModel *model in assets)
        {
            [model originImage];
        }
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.layerUtil hideLoading];
            _imageCallBck(assets);
            [self dismissViewControllerAnimated:YES completion:nil];
        });
    });
}


-(void)setShowAblumList:(BOOL)showAblumList
{
    _showAblumList = showAblumList;
    [TKIMImagePickerSet shareInstance].showAblumList = showAblumList;
}


-(void)setAllowsMultipleSelection:(BOOL)allowsMultipleSelection
{
    _allowsMultipleSelection = allowsMultipleSelection;
    [TKIMImagePickerSet shareInstance].allowsMultipleSelection = allowsMultipleSelection;
    //如果不允许多选则最多只允许选一个
    if (!allowsMultipleSelection)
    {
        [TKIMImagePickerSet shareInstance].maxAllowSelect = 1;
    }
}


-(void)setImageCallBck:(TKIMImagePickerCallBack)imageCallBck
{
    _imageCallBck = imageCallBck;
    [TKIMImagePickerSet shareInstance].imageCallBck = imageCallBck;
}


-(void)setMaxAllowSelect:(NSInteger)maxAllowSelect
{
    _maxAllowSelect = maxAllowSelect;
    //如果不允许多选则最多只允许选一个
    if (!_allowsMultipleSelection)
    {
        [TKIMImagePickerSet shareInstance].maxAllowSelect = 1;
    }
    else
    {
        [TKIMImagePickerSet shareInstance].maxAllowSelect = _maxAllowSelect;
    }
}


-(TKIMImageLayerUtil *)layerUtil
{
    if (!_layerUtil)
    {
        _layerUtil = [[TKIMImageLayerUtil alloc] init];
    }
    return _layerUtil;
}

@end
